﻿using EyFramework.Common;
using EyFramework.Common.Utils;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace EyFramework.Library
{
    /// <summary>
    /// 显示异常信息，并将弹出的异常信息保存到数据库中
    /// </summary>
    public partial class FrmExceptionMsg : FrmDevBase
    {
        private bool _sFlag = false;

        public FrmExceptionMsg(Exception ex)
        {
            InitializeComponent();
            CloseLoading();
            labError.Text = ex.Message;
            labException.Text = ex.ToString();
            //保存异常日志到数据库
            Globals.SaveException(ex);
            ShowNoDetail();
        }

        /// <summary>
        /// 按照没有异常明细的模式打开
        /// </summary>
        private void ShowNoDetail()
        {
            this.Size = new Size(433, 192);
            this.Location = new Point((Screen.PrimaryScreen.Bounds.Width - this.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2);
            btnSend.Location = new Point(87, 8);
            btnSend.Visible = false;
            btnSure.Location = new Point(167, 8);
            this.panelCenter.Visible = false;
            this.panelTop.Dock = DockStyle.Fill;
            pictureBox1.Location = new Point(8, 24);
            showBtn.Location = new Point(12, 88);
            showBtn.Text = "显示详情";
        }

        /// <summary>
        /// 按照没有异常明细的模式打开
        /// </summary>
        private void ShowDetail()
        {
            this.Size = new Size(767, 451);
            this.Location = new Point((Screen.PrimaryScreen.Bounds.Width - this.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2);
            btnSend.Location = new Point(261, 8);
            btnSend.Visible = true;
            btnSure.Location = new Point(403, 8);
            this.panelCenter.Visible = true;
            this.panelTop.Dock = DockStyle.Top;
            pictureBox1.Location = new Point(8, 9);
            showBtn.Location = new Point(12, 73);
            showBtn.Text = "隐藏详情";
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            CaptureImg();
            this.Close();
        }

        private void CaptureImg()
        {
            Image baseImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(baseImage);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            g.Dispose();
            if (!DirectoryUtil.IsExistDirectory("ScreenTemp"))
            {
                DirectoryUtil.CreateDirectory("ScreenTemp");
            }
            baseImage.Save(string.Format("ScreenTemp\\{0}.jpg", DateTime.Now.ToString("yyyyMMddHHmmss")), ImageFormat.Jpeg);
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void showBtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (_sFlag)
            {
                ShowNoDetail();
                _sFlag = false;
            }
            else
            {
                ShowDetail();
                _sFlag = true;
            }
        }
    }
}